<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>role add</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="/static/super_cmdb/css/font.css">
    <link rel="stylesheet" href="/static/super_cmdb/css/xadmin.css">

  </head>
  
  <body>
    <div class="x-body layui-anim layui-anim-up">
        <form class="layui-form" >

         <div class="layui-form-item">
              <label for="role_name" class="layui-form-label" style="width: 100px">
                  <span class="x-red">*</span>角色名称
              </label>
              <div class="layui-input-inline">
                  <input style="width: 250px;" type="text" id="role_name" name="role_name" placeholder="角色名称" required="" lay-verify="nikename"
                  autocomplete="off" class="layui-input">
              </div>
          </div>

          <div class="layui-form-item">
              <label for="L_repass" class="layui-form-label" style="width: 100px">
              </label>
              <button  class="layui-btn" lay-filter="add" lay-submit="">
                  添加
              </button>
          </div>
      </form>
    </div>
    <script type="text/javascript" src="/static/super_cmdb/js/jquery.min.js"></script>
    <script type="text/javascript" src="/static/super_cmdb/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/super_cmdb/js/xadmin.js"></script>
    <script type="text/javascript" src="/static/super_cmdb/js/jquery.cookie.js"></script>
    <script>
        $(function () {
            $.ajaxSetup({
                beforeSend:function (xhr,settings) {
                    xhr.setRequestHeader("X-CSRFtoken",$.cookie("csrftoken"));
                }
            })
        });
        layui.use(['form','layer'], function(){
            $ = layui.jquery;
            var form = layui.form
            ,layer = layui.layer;

            //自定义验证规则
            form.verify({
                nikename: function (value) {
                    if (value.length < 1) {
                        return '昵称至少得1个字符啊';
                    }
                }
                , pass: [/(.+){6,12}$/, '密码必须6到12位']
                , repass: function (value) {
                    if ($('#L_pass').val() != $('#L_repass').val()) {
                        return '两次密码不一致';
                    }
                }
            });

          form.on('submit(add)', function(data){
            //console.log(data);
            //发异步，把数据提交给django
              $.ajax({
                    url: "/super_cmdb/role_add/",
                    type: "POST",
                    data: {
                        "role_name":$("#role_name").val(),
                    },
                    success: function (data) {
                        layer.msg(data.message,{icon:1,time:60000});
                    }
                });
              console.log(data,function () {
                // 获得frame索引
                var index = parent.layer.getFrameIndex(window.name);
                //关闭当前frame
                parent.layer.close(index);
            });
            return false;
          });
        });
    </script>

  </body>

</html>